Skip to main content

πŸš€ How to Route Requests to a Specific Server for Testing

🎯 Goal​

When testing changes on a specific server behind a Load Balancer (LB), you don't want to modify all servers. Instead, you can:

  1. Use a custom header (``) to route traffic to a specific server.
  2. Modify Nginx on the target server to respond differently when this header is present.
  3. Use AWS ALB Header-Based Routing to forward requests based on custom headers.
  4. Use AWS ALB Sticky Sessions to persist a connection to a specific server.
  5. Use Host File Entry to point to a specific server for local testing.

πŸ—οΈ Option 1: Modify Nginx on the Target Server​

On your specific server, edit the Nginx configuration file:

sudo nano /etc/nginx/sites-available/default

Add this inside the server {} block:

if ($http_x_debug_server = "true") {
return 200 "βœ… This is the debug server!";
}

Save and restart Nginx:

sudo systemctl restart nginx

βœ… Now, when a request contains X-Debug-Server: true, Nginx will return a special response.


πŸ—οΈ Option 2: Use AWS ALB Header-Based Routing​

If you are using an AWS Application Load Balancer (ALB), you can set up listener rules to route requests based on a custom header.

Steps to Configure in AWS ALB:​

  1. Go to AWS Console β†’ EC2 β†’ Load Balancers.
  2. Select your ALB and navigate to the Listeners tab.
  3. Edit rules for the listener (usually port 80 or 443).
  4. Add a New Rule:
    • Click Insert Rule.
    • Add a Condition: "Header" β†’ X-Debug-Server = true
    • Add an Action: Forward to a specific target group that contains only the debug server.
  5. Save the Rule.

βœ… Now, requests with X-Debug-Server: true will go to the selected server.


πŸ—οΈ Option 3: Use AWS ALB Sticky Sessions​

AWS ALB supports sticky sessions to persist requests to the same target server using cookies.

Steps to Enable Sticky Sessions:​

  1. Go to AWS Console β†’ EC2 β†’ Target Groups.
  2. Select the target group linked to your ALB.
  3. Click the Attributes tab.
  4. Enable Stickiness and choose Duration-Based Stickiness.
  5. Set the duration (e.g., 300 seconds for 5 minutes).
  6. Save changes.

βœ… Now, once a user hits a specific server, subsequent requests will be routed to the same server for the defined duration.


πŸ—οΈ Option 4: Use Host File Entry (Local Testing)​

If you only need to test locally, you can modify the host file on your system to point a specific domain to a particular server.

Steps to Modify Host File:​

On Windows:​

  1. Open Notepad as Administrator.
  2. Open the file: C:\Windows\System32\drivers\etc\hosts
  3. Add the following entry:
    192.168.1.100  myapp.example.com
  4. Save the file.

On macOS/Linux:​

  1. Open Terminal and run:
    sudo nano /etc/hosts
  2. Add the following line:
    192.168.1.100  myapp.example.com
  3. Save the file (CTRL + X, then Y, then Enter).

βœ… Now, when you visit https://myapp.example.com, your request will go directly to 192.168.1.100.


πŸ–₯️ Step 3: Send a Request with the Custom Header​

1️⃣ Using a Browser Extension πŸŒβ€‹

Since browsers don’t allow custom headers by default, use an extension like:

Steps:​

  1. Install ModHeader.
  2. Open the extension and add a new header:
    • Header Name: X-Debug-Server
    • Header Value: true
  3. Visit your site: https://myapp.example.com
  4. πŸŽ‰ You should see: βœ… This is the debug server!

2️⃣ Using Chrome DevTools πŸ› οΈβ€‹

You can also test manually in Google Chrome:

  1. Press F12 (or Ctrl + Shift + I) to open Developer Tools.
  2. Go to the Network tab.
  3. Click on your request (myapp.example.com).
  4. Under Headers, manually add:
    X-Debug-Server: true
  5. Reload the page.

3️⃣ Using cURL (Command Line) πŸ’»β€‹

You can test using cURL on your terminal:

curl -H "X-Debug-Server: true" https://myapp.example.com

Expected output:

βœ… This is the debug server!

❓ What Happens Without the Header?​

  • If you don’t send the header, the request will be handled normally by the Load Balancer and distributed across servers.
  • If you send the header, Nginx, ALB, or the host file entry will route the request to the debug server.

πŸš€ Now you can safely test changes on a single server! πŸŽ‰


πŸ”₯ Need Help?​

If you face any issues, feel free to ask!🀝